<?php
namespace Admin\Controller;
use Think\Controller;
class ModelNav2Controller extends Controller {

	private $m='';
	public function _initialize(){
		if($this->m == ''){
			$this->m = M("Admin_nav2");
		}
	}


	//添加二级导航
	public function Nav2_Add(){
		$data['nav2_name'] = I('post.Nav2Name');
		$data['nav2_addr'] = I('post.Nav2Addr');
		$data['nav2_order'] = intval(I('post.Nav2Order'));
		$data['nav2_parent'] = intval(I('post.Nav2Parent'));
		if(!$data['nav2_name'] || !$data['nav2_addr'] || !$data['nav2_order'] || !$data['nav2_parent']){
			exit('e1');
		}
		if(!is_int($data['nav2_order']) || !is_int($data['nav2_parent'])){
			exit('e2');
		}
		$res = $this->m->where(array('nav2_addr'=>$data['nav2_addr']))->find();
		if($res){
			exit('e4');		//地址以存在！
		}
		if($this->m->create($data)){
			$st = $this->m->add();
			if($st > 0){
				$data_Rule['name'] = I('post.Nav2Addr');
				$data_Rule['title'] = I('post.Nav2Name');
				$m = M('Think_auth_rule');
				if($m->create($data_Rule)){
					$st2 = $m->add();
					if($st2){
						echo $st2;
						exit;
					}
				}
				echo 'e3';
			}else{
				exit('e2');
			}
		}else{
			exit('e3');
		}
	}



	//获取二级导航信息
	public function Nav2_Get($where = ''){
        return Get_Fpage_Ajax($this->m,$fields='',$where,$order='nav2_order',$pcount=100);
	}


	//获取二级导航全部信息
	public function Nav2_Get_All($field='',$ids=''){
		if($field && $ids){
			return $this->m->field($field)->select($ids);
		}else{
			return $this->m->select();
		}
	}


	//删除二级导航
	public function Nav2_Del(){
 		$nid = I('post.nid');
		if(!$nid){
			exit;
		}
 		$field = "nav2_addr";
 		$data = $this->Nav2_Get_All($field,$nid);
		$m = M('Think_auth_rule');
 		foreach($data as $v){
 			$name = $v['nav2_addr'];
 			$m->where(array('name'=>$name))->delete();	
 		}
		echo $this->m->delete($nid);

	}

}